﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint.Administration;

namespace SKN
{
    /// <summary>
    /// Contains common method for Web.Config
    /// </summary>
    public static class WebConfig
    {
        
        /// <summary>
        /// Initializes a new instance of the Microsoft.SharePoint.SPItemEventReceiver class.
        /// </summary>
        public static void EnableNetFramework35(SPWebApplication webApp, string owner)
        {

            var modifications = new List<SPWebConfigModification>()
                                {
                                    new SPWebConfigModification("sectionGroup[@name=\"system.web.extensions\"]",
                                                                "configuration/configSections")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModSectionGroup
                                        },

                                    new SPWebConfigModification("controls",
                                                                "configuration/system.web/pages")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModControls
                                        },

                                    new SPWebConfigModification("add[@assembly=\"System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"]",
                                                                "configuration/system.web/compilation/assemblies")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModAssemblies
                                        },

                                    new SPWebConfigModification("add[@verb=\"*\"][@path=\"*.asmx\"]",
                                                                "configuration/system.web/httpHandlers")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModHttpHandlersASMX
                                        },

                                    new SPWebConfigModification("add[@verb=\"*\"][@path=\"*_AppService.axd\"]",
                                                                "configuration/system.web/httpHandlers")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModHttpHandlersAppService
                                        },

                                    new SPWebConfigModification("add[@verb=\"GET,HEAD\"][@path=\"ScriptResource.axd\"]",
                                                                "configuration/system.web/httpHandlers")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModHttpHandlerScriptResource
                                        },

                                    new SPWebConfigModification("add[@name=\"ScriptModule\"]",
                                                                "configuration/system.web/httpModules")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModHttpHandlerScriptModule
                                        },

                                    new SPWebConfigModification("SafeControl[@Assembly=\"System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"]",
                                                                "configuration/SharePoint/SafeControls")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModSafeControl
                                        },

                                    new SPWebConfigModification("system.web.extensions",
                                                                "configuration")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModSystemWebExtensions
                                        },

                                    new SPWebConfigModification("system.webServer",
                                                                "configuration")
                                        {
                                            Owner =owner,
                                            Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode,
                                            Value = Resources.WebConfigModSystemWebServer
                                        }

                                };


            foreach (SPWebConfigModification modification in modifications)
            {
                webApp.WebConfigModifications.Add(modification);
            }

            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();
        }


        ///<summary>
        ///</summary>
        ///<param name="webApp"></param>
        ///<param name="owner"></param>
        public static void ClearAllWebConfigModifications(SPWebApplication webApp, string owner)
        {
            var modifications = new List<SPWebConfigModification>();
            foreach (SPWebConfigModification spWebConfigModification in webApp.WebConfigModifications)
            {
                if (string.Equals(spWebConfigModification.Owner, owner, StringComparison.InvariantCultureIgnoreCase))
                {
                    modifications.Add(spWebConfigModification);
                }
            }
            foreach (SPWebConfigModification configModification in modifications)
            {
                webApp.WebConfigModifications.Remove(configModification);
            }

            webApp.Update();
            webApp.WebService.ApplyWebConfigModifications();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="ownerName"></param>
        /// <param name="moduleName"></param>
        /// <param name="assemblyName"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static SPWebConfigModification CreateHttpModuleModification(string ownerName, string moduleName,
                                                                           string assemblyName, string typeName)
        {
            SPWebConfigModification modification;
            string modName = "add[@name='" + moduleName + "']";
            string ModXPath = "configuration/system.web/httpModules";
            modification = new SPWebConfigModification(modName, ModXPath);
            modification.Owner = ownerName;
            modification.Sequence = 0;
            modification.Type =
                SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
            modification.Value = @"<add name=""" + moduleName + @"""" + @"type=""" + typeName + "," + assemblyName +
                                 @""" />";
            return modification;
        }
    }
}


